package com.ustb.yygh.cmn.controller;

import com.ustb.yygh.cmn.service.DictService;
import com.ustb.yygh.common.result.Result;
import com.ustb.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ShiZhe
 * @create 2021-06-08 8:32
 */
@Api(tags = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 根据数据id查询子数据列表
     * @param id 数据id
     * @return list集合的true or false
     */
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }


    /**
     * 导出数据字典的接口
     * @param response 用户前端输入存储路径
     */
    @ApiOperation(value = "导出数据字典的接口")
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) throws IOException {
        dictService.exportDictData(response);
    }

    /**
     * 导入数据字典的接口
     * @param file xlsx文件
     * @return true or false
     */
    @ApiOperation(value = "导入数据字典的接口")
    @PostMapping("importData")
    public Result importData(MultipartFile file) throws IOException {
        dictService.importDictData(file);
        return Result.ok();
    }
}
